﻿using MediatR;
using RedDragon.Common.Domain.AuthorizeRBAC;
using RedDragon.Common.Infrastructure.Interface;
using RedDragon.RBAC.API.Application.Common.Client;
using RedDragon.RBAC.ErrorCodeEnum;
using T.Common.Domain.Common;

namespace RedDragon.RBAC.API.Application.Common.Sale
{
    public class FindSaleHandler : IRequestHandler<FindSaleCommand, Result>
    {
        /// <summary>
        /// 权限仓储
        /// </summary>
        public readonly IBaseRepository<Sales> SalessRepository;
        /// <summary>
        /// 日志
        /// </summary>
        private readonly ILogger<FindSaleCommand> logger;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="ClientsRepository"></param>
        /// <param name="logger"></param>
        public FindSaleHandler(IBaseRepository<Sales> SalessRepository, ILogger<FindSaleCommand> logger)
        {
            this.SalessRepository = SalessRepository;
            this.logger = logger;
        }
        /// <summary>
        /// 反填
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Result> Handle(FindSaleCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCode.Added };
            try
            {
                var res = await (await SalessRepository.GetAll()).Where(d => d.SalesId == request.SalesId).FirstAsync();
                if (res != null)
                {
                    result.Code = (int)CommonErrorCode.Success;
                    result.Data = res;
                    result.Msg = "根据销售id获取信息成功";
                }
            }
            catch (Exception ex)
            {
                result.Msg = "根据销售id获取信息失败" + ex.Message;
                logger.LogError("根据销售id获取信息失败" + ex.Message);
            }
            return result;
        }
    }
}
